package com.begye.smartloan.apptest.renzheng.adapter

import android.annotation.SuppressLint
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.begye.smartloan.apptest.R
import org.json.JSONObject

class FillItemSelectListDialogAdapter(
    var dataList: List<JSONObject>,
    var clickAction: (Int, JSONObject) -> Unit
) :
    RecyclerView.Adapter<FillItemSelectListDialogAdapter.ViewHolder>() {

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.list_item_fill_item_select_list_dialog, parent, false)
        return ViewHolder(view)
    }

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        private val listItemFillItemSelectListDialogTitle: TextView =
            view.findViewById(R.id.listItemFillItemSelectListDialogTitle)
        private val listItemFillItemSelectListDialogCheckedIcon: ImageView =
            view.findViewById(R.id.listItemFillItemSelectListDialogCheckedIcon)

        fun bindData(position: Int, item: JSONObject) {
            listItemFillItemSelectListDialogTitle.text = item.optString("title", "")
            if (position % 2 == 0) {
                listItemFillItemSelectListDialogTitle.setBackgroundColor(Color.parseColor("#FAFAFA"))
            } else {
                listItemFillItemSelectListDialogTitle.setBackgroundColor(Color.parseColor("#FFFFFF"))
            }

            val checked = item.optBoolean("checked", false)
            if (checked) {
                listItemFillItemSelectListDialogCheckedIcon.visibility = View.VISIBLE
            } else {
                listItemFillItemSelectListDialogCheckedIcon.visibility = View.INVISIBLE
            }

            listItemFillItemSelectListDialogTitle.setOnClickListener {
                clickAction.invoke(position, item)
            }
        }
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bindData(position, dataList[position])
    }

    override fun getItemCount(): Int {
        return dataList.size
    }
}